//
//  DHDataBaseManager.swift
//  dataBaseProj
//
//  Created by sea on 2020/1/7.
//  Copyright © 2020 CDSCRB. All rights reserved.
//

import UIKit


/// 数据库基类
var databaseManager:DHDataBaseManager? = nil

let matchTableName:String = "matchTable"

let teamTableName:String = "teamTable"

let playerTableName:String = "playerTable"

let matchDataTableName:String = "matchDataTable"


let creatMatchTableSql = "CREATE table \(matchTableName) (\(DHMatchId) Text primary key not null ,'\(DHMatchTime)' varchar(100),'\(DHMatchTeamA)' varchar(100),'\(DHMatchTeamB)' varchar(100),'\(DHMatchScoreA)' varchar(200),'\(DHMatchScoreB)' varchar(200),'\(DHMatchName)' varchar(100),'\(DHMatchStatus)' varchar(100))"


let creatTeamTableSql = "CREATE table \(teamTableName) (\(DHTeamID) Text primary key not null ,'\(DHTeamHead)' varchar(100),'\(DHTeamName)' varchar(100),'\(DHTeamWinOrLose)' varchar(200),'\(DHTeamMatchDataId)' varchar(100),'\(DHTeamPlayerCount)' integer,'\(DHTeamShouFaPlayerCount)' integer,'\(DHTeamTiBuPlayerCount)' integer)"

let creatPlayerTableSql = "CREATE table \(playerTableName) (\(DHPlayerID) Text primary key not null ,'\(DHPlayerType)' varchar(200),'\(DHPlayerMatchDataID)' varchar(100),'\(DHPlayerTeamID)' varchar(100),'\(DHPlayerName)' varchar(100),\(DHPlayerNum) varchar(200))"

let creatMatchDataTableSql  = "CREATE table \(matchDataTableName) (\(DHMatchDataID) Text primary key not null ,'\(DHMatchDataFoul)' varchar(200),'\(DHMatchDataShoot)' varchar(200),'\(DHMatchDataOffside)' varchar(200),'\(DHMatchDataRedCard)' varchar(200),'\(DHMatchDataShootIn)' varchar(200),'\(DHMatchDataDirveBall)' varchar(200),'\(DHMatchDataYellowCard)' varchar(200),'\(DHMatchDataCornnerKick)' varchar(200),'\(DHMatchDataPlayerChangeTime)' varchar(200))"

class DHDataBaseManager: NSObject {
    
    @objc static let shareDataBaseManager:DHDataBaseManager = {
        databaseManager = DHDataBaseManager()
        return databaseManager!
    }()
    
    @objc func creatTable(sql:String,tableName:String){
        
        let queue = DHDataBaseFactory.defaultManager.creatDataBase()
        
        queue.inDatabase { (db) in
            if !(DHDataBaseFactory.defaultManager.isExistWithTableNameInDataBase(name: tableName, dataBase: db)) {
                
                let isSuccess = db.executeUpdate(sql, withArgumentsIn: [])
                
                if isSuccess {
                    print("\(tableName) creat Success")
                }else{
                    print("\(tableName) creat fasle")
                }
            }else{
                print("\(tableName) is exit")
            }
        }
    }
    
    
    //mark - 增
    @objc func inserObjcWithSql(sql:String,valueArr:[String],CompeletBlock:@escaping compeletBlock){
        let queue = DHDataBaseFactory.defaultManager.creatDataBase()
        queue.inDatabase { (db) in
            
            db.open()
//            db.beginTransaction()
            do{
                try db.executeUpdate(sql, values: valueArr)
                if db.lastErrorCode() == 0{
                    CompeletBlock(NSNumber.init(value: true))
                }else{
                    CompeletBlock(NSNumber.init(value: false))
                }
            }catch{
                print("insert failed  error:\(db.lastErrorMessage()) + \(db.lastError())")
                CompeletBlock(NSNumber.init(value: false))
            }
            db.close()
        }
    }
    
    //mark - 删改
    @objc func doSomethingWithSql(sql:String,valueArr:[String],CompeletBlock:@escaping compeletBlock){
        let queue = DHDataBaseFactory.defaultManager.creatDataBase()
        queue.inDatabase { (db) in
            let isSuccss  = db.executeUpdate(sql, withArgumentsIn: [])
            if isSuccss {
                CompeletBlock(NSNumber.init(value: true))
            }else{
                print("error \(db.lastErrorMessage()))")
                CompeletBlock(NSNumber.init(value: false))

            }
        }
    }

    

    @objc func searchWithSql(sql:String,CompeletBlock:@escaping compeletBlock){
        let queue = DHDataBaseFactory.defaultManager.creatDataBase()
        queue.inDatabase { (db) in
            var rs: FMResultSet?
            do {
                try  rs = db.executeQuery(sql, values:nil)
                if db.lastErrorCode() == 0 {
                    CompeletBlock(rs!)
                }else{
                    CompeletBlock(NSNumber.init(value: false))
                }
            } catch  {
                print("error \(db.lastErrorMessage())")

                CompeletBlock(NSNumber.init(value: false))

            }
        }
    }
}








